#include "arcfour.h"

#define  F fflush(stdout)

int main(void);

void print_bin(const char *prefix_str, int8 *text, int16 len) {
    assert(len > 0);
    int16 i;
    int8 *p;
    printf("%s: '", prefix_str);
    for (i = 0, p = text; i < len; i++) {
        printf("%.02x", *p);
        if (i % 2 && i != len - 1) {
            printf(" ");
        }
        p++;
    }
    printf("'\n");
}

int main() {
    char *key = "tomatoes";
    char *clear_text = "Shall I compare thee to a summer's day?";
    int16 len_key = strlen(key);
    int16 len_text = strlen(clear_text);

    ArcFour *rc4;
    int8 *encrypted, *decrypted;

    print_bin("key       ", (int8 *) key, len_key);
    printf("clear text: '%s'\n", clear_text);
    print_bin("clear text", (int8 *) clear_text, len_text);

    // init
    printf("\ninitializing encryption... ");
    F;
    rc4 = rc4init((int8 *) key, len_key);
    printf("done.\n");

    // encrypt
    printf("\nencrypting... ");
    encrypted = rc4encrypt(rc4, (int8 *) clear_text, len_text);
    printf("done.\n");
    print_bin("encrypted ", (int8 *) encrypted, len_text);
    printf("encrypted in text: '%s'\n", encrypted);
    rc4un_init(rc4);

    // decrypt
    printf("\ndecrypting... ");
    rc4 = rc4init((int8 *) key, len_key);
    decrypted = rc4decrypt(rc4, (int8 *) encrypted, len_text);
    printf("done.\n");
    print_bin("decrypted ", (int8 *) decrypted, len_text);
    printf("decrypted in text: '%s'\n", decrypted);
    rc4un_init(rc4);

    return 0;
}